package com.miao.admin.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miao.admin.service.AdminRecommendService;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Recommend;
import com.miao.common.entity.pojo.RecommendApply;
import com.miao.admin.service.RecommendApplyService;
import com.miao.common.entity.pojo.Reduction;
import com.miao.common.entity.pojo.ReductionApply;
import com.miao.common.utils.JWTUtil;
import com.miao.websecurity.entity.Admin;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 首页推荐商品申请表(RecommendApply)表控制层
 *
 * @author MiaoWenQing
 * @since 2021-02-19 00:17:19
 */
@RestController
@RequestMapping("admin/recommendApply")
public class RecommendApplyController  {
    /**
     * 服务对象
     */
    @Resource
    private RecommendApplyService recommendApplyService;
    @Resource
    private AdminRecommendService adminRecommendService;


    /**
     * 申请
     * @param recommendApply 表单
     * @return R
     */
    @PostMapping("/apply")
    public R addApply(@RequestBody  RecommendApply recommendApply){

        recommendApply.setApplyTime(new Date());
        if (this.recommendApplyService.save(recommendApply)) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 通过申请
     * @return R
     */
    @GetMapping("/byApply")
    public R byApply(String id,@RequestHeader(value = "access-token") String token){

        RecommendApply recommendApply = this.recommendApplyService.getById(id);
        recommendApply.setOpreaTime(new Date());
        recommendApply.setAdminId(Integer.parseInt(this.getAdminId( token)));
        recommendApply.setState(2);


        Recommend recommend = this.adminRecommendService.getById(recommendApply.getPostion());
        if (null!=recommend.getRecId()){
            this.optionApply(recommend.getRecId(),this.getAdminId( token),3);
        }
        recommend.setProId(recommendApply.getProId());
        recommend.setRecId(recommendApply.getRecId());
        this.adminRecommendService.updateById(recommend);

        if (this.recommendApplyService.updateById(recommendApply)) {
            return R.ok();
        }
        return R.error();

    }


    /**
     * 获取店铺 的申请
     * @param storeId 店铺id
     * @return R
     */
    @GetMapping("/applyListStore")
    public R applyListStore(String storeId){

        QueryWrapper<RecommendApply> recommendApplyQueryWrapper = new QueryWrapper<>();
        recommendApplyQueryWrapper.eq("store_id",storeId);
        recommendApplyQueryWrapper.orderByDesc("apply_time");
        List<RecommendApply> list = this.recommendApplyService.list(recommendApplyQueryWrapper);
        return R.ok(list);

    }

    /**
     * 获取全部申请
     * @return R
     */
    @GetMapping("/applyList")
    public R applyList(){
        QueryWrapper<RecommendApply> recommendApplyQueryWrapper = new QueryWrapper<>();
        recommendApplyQueryWrapper.orderByDesc("apply_time");
        List<RecommendApply> list = this.recommendApplyService.list(recommendApplyQueryWrapper);
        return R.ok(list);
    }




    /**
     * 拒绝申请
     * @return R
     */
    @GetMapping("/refApply")
    public R refApply(String id,@RequestHeader(value = "access-token") String token){

        if (this.optionApply(id,this.getAdminId( token),1)) {
            return R.ok();
        }
        return R.error();

    }



    /**
     * 下架申请
     * @return R
     */
    @GetMapping("/offApply")
    public R offApply(String id,@RequestHeader(value = "access-token") String token){
        if (this.optionApply(id,this.getAdminId( token),3)) {
            return R.ok();
        }
        return R.error();

    }




    public boolean optionApply(String id , String adminId, int state){
        RecommendApply recommendApply = this.recommendApplyService.getById(id);
        recommendApply.setOpreaTime(new Date());
        recommendApply.setAdminId(Integer.parseInt(adminId));
        recommendApply.setState(state);
       return this.recommendApplyService.updateById(recommendApply);

    }

    private String getAdminId(String token) {
        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        return map.get("adminId").toString();//1平台 0店铺


    }


}
